package com.jumi.microservice.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jumi.microservice.common.base.dto.ResponseResult;
import com.jumi.microservice.common.utils.ResultGenerator;
import com.jumi.microservice.domain.FreightTemplateExtendDO;
import com.jumi.microservice.service.FreightTemplateExtendService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * <p>
 * 运费模板详情表 前端控制器
 * </p>
 *
 * @author liuz
 * @since 2020-07-27
 */
@RestController
@Api(tags = "运费模板详情表 相关接口")
@RequestMapping("/commodity/freight/template/extend")
public class FreightTemplateExtendController {

    @Autowired
    private FreightTemplateExtendService freightTemplateExtendService;


    @ApiOperation(value = "新增数据", notes = "新增一条运费模板详情表数据")
    @PostMapping("/add")
    public ResponseResult<FreightTemplateExtendDO> add(FreightTemplateExtendDO freightTemplateExtend) {
        freightTemplateExtend.setCreateTime(LocalDateTime.now());
        freightTemplateExtend.setUpdateTime(LocalDateTime.now());
        freightTemplateExtendService.save(freightTemplateExtend);
        return ResultGenerator.genSuccessResult(freightTemplateExtend);
    }


    @ApiOperation(value = "删除数据", notes = "删除指定ID运费模板详情表数据")
    @ApiImplicitParam(name = "id", value = "ID")
    @DeleteMapping("/delete")
    public ResponseResult delete(@RequestParam Integer id) {
        freightTemplateExtendService.removeById(id);
        return ResultGenerator.genSuccessResult();
    }


    @ApiOperation(value = "更新数据", notes = "更新指定ID运费模板详情表数据")
    @PutMapping("/update")
    public ResponseResult<FreightTemplateExtendDO> update(FreightTemplateExtendDO freightTemplateExtend) {
        freightTemplateExtend.setUpdateTime(LocalDateTime.now());
        freightTemplateExtendService.updateById(freightTemplateExtend);
        return ResultGenerator.genSuccessResult(freightTemplateExtend);
    }


    @ApiOperation(value = "查询数据", notes = "获取指定ID运费模板详情表数据")
    @ApiImplicitParam(name = "id", value = "ID")
    @GetMapping("/detail")
    public ResponseResult<FreightTemplateExtendDO> detail(@RequestParam Integer id) {
        FreightTemplateExtendDO freightTemplateExtend = freightTemplateExtendService.getById(id);
        return ResultGenerator.genSuccessResult(freightTemplateExtend);
    }


    @ApiOperation(value = "查询分页", notes = "获取运费模板详情表分页列表数据")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "page", value = "当前页码"),
        @ApiImplicitParam(name = "size", value = "分页大小"),
    })
    @GetMapping("/list")
    public ResponseResult<IPage<FreightTemplateExtendDO>> list(
            @RequestParam(defaultValue = "0") Integer page,
            @RequestParam(defaultValue = "0") Integer size,
            FreightTemplateExtendDO freightTemplateExtend) {
        LambdaQueryWrapper<FreightTemplateExtendDO> wrapper = Wrappers.lambdaQuery();

        wrapper.orderByDesc(FreightTemplateExtendDO::getId);
        IPage<FreightTemplateExtendDO> pageInfo = freightTemplateExtendService.page(
                new Page<>(page, size),wrapper);
        return ResultGenerator.genSuccessResult(pageInfo);
    }


}
